#include "TextureManager.h"

namespace Lame3d
{
	const char* _textureExtensions[] = {"PNG", "TIF", "TGA", "JPG", NULL};
	
	TextureManager::TextureManager(const char* path)
	{
		Directory directory(path, _textureExtensions);
		
		Texture *nT;
		std::map<std::string, File*>::const_iterator it;
		for(it = directory.FileIterator(); it != directory.End(); ++it) {
			printf("added texture: %s\n", it->first.c_str());
			nT = new Texture(*it->second);
			textures[it->first] = nT;
		}
	}
	
	TextureManager::~TextureManager()
	{
		std::map<std::string, Texture*>::iterator it;
		for(it = textures.begin(); it != textures.end(); ++it) {
			delete it->second;
		}
	}
	
	Texture& TextureManager::operator[](const std::string &texName)
	{
		std::map<std::string, Texture*>::iterator it;
		it = textures.find(texName);
		if(it == textures.end()) {
			printf("Attempted to load nonexistant texture: %s\n", texName.c_str());
			throw "Attempted to find nonexistant texture.";
		} else {
			return *it->second;
		}
	}
}
